package part1.solution6;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: shaochong
 * @Date: 2021/8/7
 * @Description:
 */
public class Solution6 {
    public static String convert(String s, int numRows) {
        if (numRows == 1) return s;
        if (s.length() == 1) return s;

        List<Character>[] arr = new List[numRows];
        for (int i = 0; i < numRows; i++) {
            arr[i] = new ArrayList<Character>();
        }

        char[] chars = s.toCharArray();

        int currentRow = 0;
        int direction = 1;

        for (int i = 0; i < chars.length; i++) {
            arr[currentRow].add(chars[i]);
            if (direction > 0) {
                if (currentRow == numRows -1 ) {
                    currentRow--;
                    direction = 0;
                } else {
                    currentRow++;
                }
            } else {
                if (currentRow == 0 ) {
                    currentRow++;
                    direction = 1;
                } else {
                    currentRow--;
                }
            }
        }
        char[] ans = new char[s.length()];
        int j =0;
        for (int i = 0; i < arr.length; i++) {
            for (char c : arr[i]) {
                ans[j++] = c;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ans.length; i++) {
            sb.append(ans[i]);
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        String ans = convert("PAYPALISHIRING", 3);
        System.out.println(ans);
    }
}
